package com.kdgc.power.fw.service;

import com.kdgc.power.fw.util.FenUtil;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ChessService  {

    public String fenToSvg(String fen) throws IOException {
        List<Map<String,Object>> allChess=new ArrayList<>();
        String chessString = FenUtil.fenToString(fen);
        for (int i = 0; i < Math.min(90,chessString.length()); i++) {
            Map<String,Object> chessInfo=new HashMap<>();
            char ch = chessString.charAt(i);
            if (ch != '0') {
                int row = i / 9;
                int col = i % 9;
                chessInfo.put("chess",ch);
                chessInfo.put("row",col*40+20);
                chessInfo.put("col",row*40+20);
                allChess.add(chessInfo);
            }
        }
        ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader("/templates");
        Configuration configuration = Configuration.defaultConfiguration();
        GroupTemplate gt = new GroupTemplate(resourceLoader, configuration);
        Template template =gt. getTemplate("chess.btl");
        template.binding("allChess", allChess);
        // 4. 渲染模板并获取生成的字符串
        return  template.render( );



    }
}
